<?php
namespace app\api\service;
use think\Request;
use app\api\model\Alliance;
use app\api\model\Account;
use app\api\model\Contact;
use app\api\model\Yj;
class AllianceService {

    // 获取一条加盟申请信息
    public function one($id) {
        return show(1, 'ok', Alliance::with(['fcontact'])->find($id), 200);
    }
    // 获取意见
    public function getYj($id) {
        if (!$id) {
            return show(0, '操作失败', [], 200);
        }
        $data = Yj::all(function($query) use ($id) {
            $query->where('alliance_id', $id)->order('id', 'asc');
        });
        return show(1, 'ok', $data, 200);
    }

    public function page($page = 1, $size = 10){
        $data 	= Request::instance()->get();
        $where 	= [];
        if (!isset($data['status']) || !is_numeric($data['status'])) {
            return [];
        }
        $where['status'] = $data['status']; // 审核状态

        empty($data['name']) || $where['name'] = ['like','%'.$data['name']];

        $obj = Alliance::with(['fcontact'])->where($where)
        ->paginate($size, false, ['page' => $page]);

        if ($obj->isEmpty()){
            return [];
        }
        return $obj->toArray();
    }

    // 审核
    public function check() {
        $param = Request::instance()->param();	//获取参数
        $check = array(3, 4); // 3 未通过、4 通过
        if (!$param['id'] || !is_numeric($param['id']) || !in_array($param['status'], $check) || !isset($param['user'])) {
            return show(0, '操作失败', [], 200);
        }
        $obj = Alliance::get($param['id']);

        $obj->status = $param['status'];
        $obj->check_user = $param['user'];

        // 创建账户
        if ($param['status'] == 4) {
            $acc = new Account;
            $acc->username = $obj->account; 
            $acc->password = md5($obj->account . 'aa');
            $acc->alliance_id = $obj->id;
            $acc->type = 2;
            $acc->save();
        }
        // 意见表写入
        $yj = new Yj;
        $yj->alliance_id = $param['id'];
        $yj->content = $param['content'];

        if( $obj->save() && $yj->save() ){
            return show(1, '操作成功', [], 200);
        }

        return show(0, '操作失败', [], 200);
    }

    // 提交审核
    public function checkIn() {
        $param = Request::instance()->param();	//获取参数
        if (!$param['id'] || !is_numeric($param['id'])) {
            return show(0, '操作失败', [], 200);
        }
        $obj = Alliance::get($param['id']);

        $obj->status = 2;

        if( $obj->save() ){
            return show(1, '操作成功', [], 200);
        }

        return show(0, '操作失败', [], 200);
    }

    // 申请加盟
    public function add() {
        $param = Request::instance()->param();	//获取参数
        // 新增
        $obj = new Alliance;

        $contact = new Contact;

        try {
            $contact->name = $param['lxname'];
            $contact->position = $param['position'];
            $contact->mobile = $param['mobile'];
            $contact->email = $param['email'];
            $contact->qq = $param['qq'];
            $contact->address = $param['address'];
        }catch(Exption $e) {
            return show(0, '修改失败', [], 200);
        }

        if (!$contact->save() || !$contact->id) {
            return show(0, '修改失败', [], 200);
        }

        try {
            $obj->name = $param['name'];
            $obj->franchise = $param['franchise'];
            $obj->expire = strtotime($param['expire']);
            $obj->status = 1;
            $obj->create_user = $param['create_user'];
            $obj->accessory = $param['accessory'];

            $obj->account = $param['account'];
            $obj->province = $param['province'];
            $obj->city = $param['city'];
            $obj->district = $param['district'];
            $obj->detail = $param['detail'];
            $obj->location = $param['location'];

            $obj->contact = $contact->id;

        }catch(Exption $e) {
            return show(0, '修改失败', [], 200);
        }
        if( $obj->save()){
            return show(1, '提交成功', [], 200);
        }
    }

    // 更新
    public function save(){ 
        $param = Request::instance()->param();	//获取参数

        $obj = Alliance::get($param['id']);
        try {
            $obj->name = $param['name'];
            $obj->franchise = $param['franchise'];
            $obj->expire = strtotime($param['expire']);
            $obj->status = 1;
            $obj->create_user = $param['create_user'];
            $obj->accessory = $param['accessory'];
        }catch(Exption $e) {
            return show(0, '修改失败', [], 200);
        }
        // 修改通讯信息
        $contact = Contact::get($obj->contact);
        try {
            $contact->name = $param['lxname'];
            $contact->position = $param['position'];
            $contact->mobile = $param['mobile'];
            $contact->email = $param['email'];
            $contact->qq = $param['qq'];
            $contact->address = $param['address'];
        }catch(Exption $e) {
            return show(0, '修改失败', [], 200);
        }

        // 检测错误
        if( $obj->save() && $contact->save() ){
            return show(1, '修改信息成功', [], 200);
        }
        
        return show(0, '修改失败', [], 200);
    }

    // 删除
    public function delete($id){

    	if( Alliance::destroy($id, true) ){
            return show(1, '删除成功', [], 200);
        }
        
        return show(0, '删除失败', [], 200);
    }

    // 获取公司列表
    public function getList(){
        
        return show(1, 'ok', Alliance::with(['fcontact'])
        ->where('status', 4)
        ->select(), 200);
    }
    public function getOpen($alliance_id) {
        $obj = Alliance::get($alliance_id);
        return show(1, 'ok', $obj->open, 200);
    }

    public function setOpen($alliance_id) {
        $obj = Alliance::get($alliance_id);
        if ($obj->open == 0 ) {
            $obj->open = 1;
        }else {
            $obj->open = 0; 
        }
        $obj->save();
        return show(1, 'ok', [], 200);
    }
}